import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserInfoDao {

    /**
     * 插入用户
     * @param userInfo
     */
    public void insert(UserInfo userInfo) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构造 sql
            String sql = "insert into userinfo values(null, ?, ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, userInfo.getUsername());
            statement.setString(2, userInfo.getPassword());
            //执行 sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                System.out.println("用户" + userInfo.getUsername() + "注册成功!");
            } else {
                System.out.println("用户" + userInfo.getUsername() + "注册失败!");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement);
        }

    }

}
